﻿using Apewer.Internals;

#if NETFRAMEWORK
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using static Apewer.Source.SourceUtility;
#endif

namespace Apewer.Source
{

    /// <summary>连接 Access 数据库的客户端。</summary>
    public abstract partial class Access
    {

        /// <summary>尝试解析 Access 文件的密码。</summary>
        /// <remarks>当操作系统启用随机化内存分配时，此方法可能会产生异常。</remarks>
        public static string[] ParsePassword(string path) => AccessHelper.GetPassword(path);

        /// <summary>数据库文件路径。</summary>
        public virtual string Path { get; protected set; }

    }

#if NETFRAMEWORK

    public abstract partial class Access : DbClient
    {

        #region 连接

        OleDbConnection _conn = null;
        string _connstr = null;

        /// <summary>构造函数。</summary>
        internal Access(string connectrionString, Timeout timeout = null) : base(timeout)
        {
            _connstr = connectrionString;
        }

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public Access(IDbConnection connection, Timeout timeout = null) : base(timeout)
        {
            if (connection == null) throw new ArgumentNullException(nameof(connection), "指定的连接无效。");

            var conn = connection as OleDbConnection;
            if (conn == null) throw new ArgumentException(nameof(connection), "指定的连接不是支持的类型。");

            _conn = conn;
            _connstr = conn.ConnectionString;
        }

        #endregion

        #region public

        /// <summary></summary>
        public override string[] ColumnNames(string tableName)
        {
            if (_conn == null) _conn = new OleDbConnection(_connstr);

            // OleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            var table = _conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName.ToString(), null });
            using (var query = new Query(table))
            {
                var names = new ArrayBuilder<string>();
                for (var i = 0; i < query.Rows; i++)
                {
                    names.Add(query.Text(i, "COLUMN_NAME"));
                }
                return names.Export();
            }
        }

        /// <summary></summary>
        public override string[] StoreNames() => throw new InvalidOperationException();

        /// <summary></summary>
        public override string[] TableNames()
        {
            // QueryStrings("select name from msysobjects where type=1 and flags = 0");
            Connect();
            var schema = _conn.GetSchema("Tables");
            using (var query = new Query(schema))
            {
                var json = query.ToJson();
                var names = new List<string>();
                for (var i = 0; i < query.Rows; i++)
                {
                    var type = query.Text(i, "TABLE_TYPE");
                    var name = query.Text(i, "TABLE_NAME");
                    switch (type)
                    {
                        case "ACCESS TABLE":
                        case "SYSTEM TABLE":
                            break;
                        case "TABLE":
                            names.Add(name);
                            break;
                    }
                }
                names.Sort();
                return names.ToArray();
            }
        }

        /// <summary></summary>
        public override string Insert(object record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust) FixProperties(record);

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性。
                if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var names = new List<string>(psc);
            var values = new List<string>(psc);
            foreach (var column in ps)
            {
                //names.Add(TextGenerator.Merge("[", column, "]"));
                names.Add(TextUtility.Merge(column));
                values.Add("@" + column);
            }
            var sb = new StringBuilder();
            sb.Append("insert into ", table, "(", string.Join(", ", names.ToArray()), ") ");
            sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
            var sql = sb.ToString();

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        /// <summary></summary>
        public override string Update(IRecord record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust)
            {
                FixProperties(record);
                SetUpdated(record);
            }

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            if (structure.Key != null) excluded.Add(structure.Key.Field);
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性、自增属性和主键属性。
                if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var items = new List<string>();
            foreach (var p in ps)
            {
                var pn = p.ParameterName;
                items.Add(TextUtility.Merge("[", pn, "] = @", pn));
            }
            var key = record.Key.SafeKey();
            var sql = $"update {table} set {string.Join(", ", items.ToArray())} where [{structure.Key.Field}]='{key}'";

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        #endregion

        #region protected

        /// <summary></summary>
        public override string ConnectionString { get => _connstr; }

        /// <summary></summary>
        protected override IDbConnection GetConnection()
        {
            if (_conn == null) _conn = new OleDbConnection(_connstr);
            return _conn;
        }

        /// <summary></summary>
        protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new OleDbDataAdapter((OleDbCommand)command);

        /// <summary></summary>
        protected override IDataParameter CreateParameter() => new OleDbParameter();

        /// <summary></summary>
        protected override string Initialize(TableStructure structure, string table)
        {
            var model = structure.Model;
            if (table.IsEmpty()) table = structure.TableName;

            // 检查现存表。
            var exists = false;
            var tables = TableNames();
            if (tables.Length > 0)
            {
                var lower = table.ToLower();
                foreach (var tn in tables)
                {
                    if (TextUtility.IsEmpty(tn)) continue;
                    if (tn.ToLower() == lower)
                    {
                        exists = true;
                        break;
                    }
                }
            }

            if (exists)
            {
                // 获取已存在的列名。
                var columns = ColumnNames(table);
                if (columns.Length > 0)
                {
                    var lower = new List<string>();
                    foreach (var column in columns)
                    {
                        if (TextUtility.IsBlank(column)) continue;
                        lower.Add(column.ToLower());
                    }
                    columns = lower.ToArray();
                }

                // 增加列。
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    // 去重。
                    var lower = column.Field.ToLower();
                    if (columns.Contains(lower)) continue;

                    var type = Declaration(column);
                    if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");

                    var sql = TextUtility.Merge("alter table [", table, "] add ", type, "; ");
                    var execute = Execute(sql, null, false);
                    if (execute.Success == false) return execute.Message;
                }
                return TextUtility.Empty;
            }
            else
            {
                var sqlcolumns = new List<string>();
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    var type = Declaration(column);
                    if (!column.Independent)
                    {
                        if (column.PrimaryKey) type = type + " primary key";
                        if (column.Incremental) type += " identity";
                    }

                    if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
                    sqlcolumns.Add(type);
                }
                if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表：没有定义任何字段。";
                var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
                var execute = Execute(sql, null, false);
                if (execute.Success) return TextUtility.Empty;
                return execute.Message;
            }

        }

        /// <summary></summary>
        protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
            else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
        }

        /// <summary></summary>
        protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
            else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
        }

        /// <summary></summary>
        protected override string List(string tableName, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select * from [{tableName}]";
            else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
        }

        #endregion

        #region Parameter

        /// <summary>创建参数。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        static OleDbParameter Parameter(Parameter parameter)
        {
            if (parameter == null) throw new InvalidOperationException("参数无效。");
            return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
        }

        /// <summary>创建参数。</summary>
        public static OleDbParameter Parameter(string name, ColumnType type, int size, object value)
        {
            var vname = TextUtility.Trim(name);
            if (TextUtility.IsBlank(vname)) return null;

            var vtype = OleDbType.BigInt;
            switch (type)
            {
                case ColumnType.Boolean:
                    vtype = OleDbType.Boolean;
                    break;
                case ColumnType.Bytes:
                    vtype = OleDbType.LongVarBinary;
                    break;
                case ColumnType.Integer:
                    vtype = OleDbType.Integer;
                    break;
                case ColumnType.Float:
                    vtype = OleDbType.Double;
                    break;
                case ColumnType.DateTime:
                    vtype = OleDbType.Date;
                    break;
                case ColumnType.VarChar:
                case ColumnType.VarChar191:
                case ColumnType.VarCharMax:
                    vtype = OleDbType.VarChar;
                    break;
                case ColumnType.NVarChar:
                case ColumnType.NVarChar191:
                case ColumnType.NVarCharMax:
                    vtype = OleDbType.VarWChar;
                    break;
                case ColumnType.Text:
                    vtype = OleDbType.LongVarChar;
                    break;
                case ColumnType.NText:
                    vtype = OleDbType.LongVarWChar;
                    break;
                default:
                    throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
            }

            var vsize = size;
            switch (type)
            {
                case ColumnType.VarChar:
                    vsize = NumberUtility.Restrict(vsize, 0, 8000);
                    break;
                case ColumnType.NVarChar:
                    vsize = NumberUtility.Restrict(vsize, 0, 4000);
                    break;
                case ColumnType.VarChar191:
                case ColumnType.NVarChar191:
                    vsize = NumberUtility.Restrict(vsize, 0, 191);
                    break;
                default:
                    vsize = 0;
                    break;
            }

            var vvalue = value;
            if (vvalue is string && vvalue != null && vsize > 0)
            {
                vvalue = TextUtility.Left((string)vvalue, vsize);
            }

            var parameter = new OleDbParameter();
            parameter.ParameterName = vname;
            parameter.OleDbType = vtype;
            parameter.Value = vvalue;
            if (vsize > 0) parameter.Size = vsize;
            return parameter;
        }

        /// <summary>创建参数。</summary>
        public static IDbDataParameter CreateParameter(String field, DbType type, Int32 size, Object value)
        {
            var p = new OleDbParameter();
            p.ParameterName = field;
            p.DbType = type;
            p.Size = size;
            p.Value = value;
            return p;
        }

        /// <summary>创建参数。</summary>
        public static IDbDataParameter CreateParameter(String field, DbType type, Object value)
        {
            var p = new OleDbParameter();
            p.ParameterName = field;
            p.DbType = type;
            p.Value = value;
            return p;
        }

        #endregion

        #region protected

        /// <summary>获取或设置连接字符串。</summary>
        /// <exception cref="FileNotFoundException"></exception>
        internal protected static string GenerateCS(string provider, string path, string pass, string jo)
        {
            if (!File.Exists(path)) throw new FileNotFoundException("文件不存在。", path);

            var sb = new StringBuilder();

            sb.Append("provider=", provider, "; ");

            if (!string.IsNullOrEmpty(path)) sb.Append("data source=", path, "; ");

            if (string.IsNullOrEmpty(pass)) sb.Append("persist security info=false; ");
            else sb.Append("jet oledb:database password=\"", pass, "\"; ");

            // Microsoft Access Workgroup Information
            if (!string.IsNullOrEmpty(jo)) sb.Append("jet oledb:system database=", jo, "; ");

            return sb.ToString();
        }

        #endregion

        #region static

        static string Declaration(ColumnAttribute column)
        {
            var type = TextUtility.Empty;
            var vcolumn = column;
            var length = Math.Max(0, vcolumn.Length);
            switch (vcolumn.Type)
            {
                case ColumnType.Boolean:
                    type = "bit";
                    break;
                case ColumnType.Integer:
                    type = "money";
                    break;
                case ColumnType.Float:
                    type = "float";
                    break;
                case ColumnType.Bytes:
                    type = "binary";
                    break;
                case ColumnType.DateTime:
                    type = "datetime";
                    break;
                case ColumnType.VarChar:
                    type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
                    break;
                case ColumnType.VarChar191:
                    type = TextUtility.Merge("varchar(191)");
                    break;
                case ColumnType.VarCharMax:
                    type = TextUtility.Merge("varchar(max)");
                    break;
                case ColumnType.Text:
                    type = TextUtility.Merge("text");
                    break;
                case ColumnType.NVarChar:
                    type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
                    break;
                case ColumnType.NVarChar191:
                    type = TextUtility.Merge("nvarchar(191)");
                    break;
                case ColumnType.NVarCharMax:
                    type = TextUtility.Merge("nvarchar(max)");
                    break;
                case ColumnType.NText:
                    type = TextUtility.Merge("ntext");
                    break;
                default:
                    return TextUtility.Empty;
            }
            return TextUtility.Merge("[", vcolumn.Field, "] ", type);
        }

        #endregion

    }

    /// <summary>使用 Microsoft.Jet.OLEDB.4.0 访问 Access 97 - 2003 数据库文件。</summary>
    public class AccessJet4 : Access
    {

        const string JetOleDB4 = "microsoft.jet.oledb.4.0";

        /// <summary>创建 Access 类的新实例。</summary>
        /// <exception cref="FileNotFoundException"></exception>
        public AccessJet4(string path, string pass = null, string jo = null, Timeout timeout = null)
            : base(GenerateCS(JetOleDB4, path, pass, jo), timeout)
        {
            Path = path;
        }

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public AccessJet4(IDbConnection connection, Timeout timeout = null) : base(connection, timeout)
        {
            Path = (connection as Access)?.Path;
        }

        /// <summary>生成新的 Standard Jet DB 数据库。</summary>
        public static byte[] NewJetDB()
        {
            using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Apewer.JETDB.gz"))
            {
                using (var gzip = new GZipStream(resource, CompressionMode.Decompress, false))
                {
                    using (var memory = new MemoryStream())
                    {
                        BytesUtility.Read(gzip, memory, null, 1024);
                        return memory.ToArray();
                    }
                }
            }
        }

    }

    /// <summary>使用 Microsoft.ACE.OLEDB.12.0 访问 Access 2007 数据库文件。</summary>
    public class AccessAce12 : Access
    {

        const string AceOleDB12 = "microsoft.ace.oledb.12.0";

        /// <summary>创建 Access 类的新实例。</summary>
        /// <exception cref="FileNotFoundException"></exception>
        public AccessAce12(string path, string pass = null, string jo = null, Timeout timeout = null)
            : base(GenerateCS(AceOleDB12, path, pass, jo), timeout)
        {
            Path = path;
        }

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public AccessAce12(IDbConnection connection, Timeout timeout = null) : base(connection, timeout)
        {
            Path = (connection as Access)?.Path;
        }

        /// <summary>生成新的 Standard ACE DB 数据库。</summary>
        public static byte[] NewAceDB()
        {
            using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Apewer.ACEDB.gz"))
            {
                using (var gzip = new GZipStream(resource, CompressionMode.Decompress, false))
                {
                    using (var memory = new MemoryStream())
                    {
                        BytesUtility.Read(gzip, memory, null, 1024);
                        return memory.ToArray();
                    }
                }
            }
        }

    }

#endif

}
